<?php
namespace App\Service;

class HwcwService extends BaseService
{
    //货物仓位

    //列表
    public static function index($cw_id)
    {
        // 货架信息
        $shelfOne = (array) self::dbConn(self::$dbShelf)
            ->where('id', $cw_id)
            ->first();
        $shelf_name = empty($shelfOne) ? '' : $shelfOne['title'];
        // 仓位信息
        $res = self::dbConn(self::$dbCangwei)
            ->select('id', 'title')
            ->where('del', 0)
            ->where('pid', 0)
            ->where('shelf_id', $cw_id)
            ->get();
        if (!count($res)) {
            return self::toback(0, '', []);
        }
        $arys = [];
        foreach ($res as $v) {
            $cw_name = $shelf_name.'货架-'.$v->title.'层';
            // 层货物
            $arys[] = [
                'id' => $v->id, 'title' => $cw_name,
                'hwList' => self::hwcw($v->id),
            ];
            // 仓货物
            $cwOnes = self::dbConn(self::$dbCangwei)
                ->where('del', 0)
                ->where('pid', $v->id)
                ->get();
            $cw2_name = '';
            if (count($cwOnes)) {
                foreach ($cwOnes as $vv) {
                    $arys[] = [
                        'id' => $vv->id, 
                        'title' => $cw_name.'-'.$vv->title.'仓',
                        'hwList' => self::hwcw($vv->id),
                    ];
                }
            }
        }
        return self::toback(0, '', $arys);
    }

    // 获取货物仓位
    public static function hwcw($cw_id)
    {
        $hwcwOnes = self::dbConn(self::$dbHwcw)
            ->where('del', 0)
            ->where('cw_id', $cw_id)
            ->get();
        if (!count($hwcwOnes)) {
            return [];
        }
        $arys = [];
        foreach ($hwcwOnes as $hwcw) {
            $hwOne = (array) self::dbConn(self::$dbHuowu)
                ->where('id', $hwcw->hw_id)
                ->first();
            $hw_name = '';
            $hw_code = '';
            $imgs = [];
            if (!empty($hwOne)) {
                $hw_name = $hwOne['title'];
                $hw_code = $hwOne['hw_code'];
                if ($hwOne['imgs']) {
                    $imgs = explode(';', $hwOne['imgs']);
                }
            }
            $arys[] = [
                'id' => $hwcw->id, 'title' => $hw_name,
                'hw_code' => $hw_code, 'stock' => $hwcw->stock,
                'imgList' => $imgs,
            ];
        }
        return $arys;
    }

    // 选项
    public static function getOption()
    {
        $arys = ['hw' => []];
        $hwOnes = self::dbConn(self::$dbHuowu)
            // ->select('id', 'title')
            ->where('del', 0)
            ->get();
        if (count($hwOnes)) {
            foreach ($hwOnes as $v) {
                $arys['hw'][] = [
                    'id' => $v->id, 
                    'title' => $v->title.'-'.$v->hw_code,
                ];
            }
        }
        return self::toback(0, '', $arys);
    }

    // 选项
    public static function getOne($id)
    {
        $res = (array) self::dbConn(self::$dbHuowu)
            ->where('id', $id)
            ->first();
        if (empty($res)) {
            return self::toback(-2, '没有数据');
        }
        return self::toback(0, '', $res);
    }

    // 入仓
    public static function cangIn($cw_id, $hw_code, $num)
    {
        $hwOne = (array) self::dbConn(self::$dbHuowu)
            ->where('hw_code', $hw_code)
            ->first();
        if (empty($hwOne)) {
            return self::toback(-2, '没有货物');
        }
        $cangOne = (array) self::dbConn(self::$dbCangwei)
            ->where('id', $cw_id)
            ->first();
        if (empty($cangOne)) {
            return self::toback(-2, '没有仓位');
        }
        $res = (array) self::dbConn(self::$dbHwcw)
            ->where('del', 0)
            ->where('cw_id', $cw_id)
            ->where('hw_id', $hwOne['id'])
            ->first();
        if (empty($res)) {
            $pas = [
                'hw_id' => $hwOne['id'], 'cw_id' => $cw_id,
                'stock' => $num, 'addtime' => toDate(), 'uptime' => toDate(),
            ];
            self::dbConn(self::$dbHwcw)->insertGetId($pas);
        } else {
            $pas = [
                'stock' => $res['stock'] + $num,
                'uptime' => toDate(),
            ];
            self::dbConn(self::$dbHwcw)
                ->where('id', $res['id'])
                ->update($pas);
        }
        self::dbConn(self::$dbHwMove)->insertGetId([
            'hw_id' => $hwOne['id'], 'cw_start' => 0,
            'cw_end' => $cw_id, 'num' => $num,
            'addtime' => toDate(),
        ]);
        return self::toback();
    }

    // 减仓
    public static function cangDecre($id, $num)
    {
        $res = (array) self::dbConn(self::$dbHwcw)
            ->where('id', $id)
            ->first();
        if (empty($res)) {
            return self::toback(-2, '没有库存');
        }
        if ($res['stock'] < $num) {
            return self::toback(-2, '库存不足');
        }
        self::dbConn(self::$dbHwcw)
            ->where('id', $id)
            ->update([
                'stock' => $res['stock'] - $num,
                'uptime' => toDate(),
            ]);
        self::dbConn(self::$dbHwMove)->insertGetId([
            'hw_id' => $res['hw_id'], 'cw_start' => $res['cw_id'],
            'cw_end' => 0, 'num' => $num,
            'addtime' => toDate(),
        ]);
        return self::toback();
    }

    // 加仓
    public static function cangIncre($id, $num)
    {
        $res = (array) self::dbConn(self::$dbHwcw)
            ->where('id', $id)
            ->first();
        if (empty($res)) {
            return self::toback(-2, '没有库存');
        }
        self::dbConn(self::$dbHwcw)
            ->where('id', $id)
            ->update([
                'stock' => $res['stock'] + $num,
                'uptime' => toDate(),
            ]);
        self::dbConn(self::$dbHwMove)->insertGetId([
            'hw_id' => $res['hw_id'], 'cw_start' => 0,
            'cw_end' => $res['cw_id'], 'num' => $num,
            'addtime' => toDate(),
        ]);
        return self::toback();
    }
}